local guni = fk.CreateSkill {
  name = "lingling__guni",
}

Fk:loadTranslationTable{
  ["lingling__guni"] = "锢溺",
  [":lingling__guni"] = "回合结束时，你可以令一名本回合受到过伤害的角色选择：摸X张牌并翻面；你摸X张牌（X为本回合进入弃牌堆牌的花色数）。",

  ["#lingling__guni-choose"] = "锢溺：你可以令一名角色选择：摸%arg张牌并翻面，或你摸%arg张牌",
  ["lingling__guni_turnover"] = "摸%arg张牌并翻面",
  ["lingling__guni_draw"] = "%src摸%arg张牌",

  ["@lingling__guni-turn"] = "锢溺",

  ["#lingling__yansong_EasterEgg"] = "严阁老八十大寿的时候，一柱擎起大明天，那句诗不知是谁作的？",
}

guni:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(guni.name) and
      #player.room.logic:getActualDamageEvents(1, function (e)
        return not e.data.to.dead
      end, Player.HistoryTurn) > 0 and
      #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).suit ~= Card.NoSuit then
                return true
              end
            end
          end
        end
      end, Player.HistoryTurn) > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = {}
    room.logic:getActualDamageEvents(1, function (e)
      table.insertIfNeed(targets, e.data.to)
    end, Player.HistoryTurn)
    local suits = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(suits, Fk:getCardById(info.cardId).suit)
          end
        end
      end
    end, Player.HistoryTurn)
    table.removeOne(suits, Card.NoSuit)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = guni.name,
      prompt = "#lingling__guni-choose:::"..#suits,
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to, choice = #suits})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local n = event:getCostData(self).choice
    local choice = room:askToChoice(to, {
      choices = {
        "lingling__guni_turnover:::"..n,
        "lingling__guni_draw:"..player.id.."::"..n,
      },
      skill_name = guni.name,
    })
    if choice:startsWith("lingling__guni_turnover") then
      to:drawCards(n, guni.name)
      if not to.dead then
        to:turnOver()
      end
    else
      player:drawCards(n, guni.name)
    end
  end,
})

guni:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(guni.name, true) and player.room.current == player and
      #player:getTableMark("@lingling__guni-turn") < 4 then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local mark = player:getTableMark("@lingling__guni-turn")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          table.insertIfNeed(mark, Fk:getCardById(info.cardId):getSuitString(true))
        end
      end
    end
    table.removeOne(mark, "log_nosuit")
    player.room:setPlayerMark(player, "@lingling__guni-turn", mark)
  end,
})

guni:addEffect(fk.Death,{
  can_refresh = function (self, event, target, player, data)
    return target == player and player.general == "lingling__yansong" and table.find(player.room.alive_players,function (cp)
      return cp.general ~= "lingling__yansong" and cp.kingdom == "ming"
    end)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:sendLog{
      type = "#lingling__yansong_EasterEgg",
      from = player.id,
      toast = true,
    }
    room:notifySkillInvoked(player, "lingling__luanwu", "big")
    room:doIndicate(player, room:getOtherPlayers(player, false))
    for _, to in ipairs(room:getOtherPlayers(player)) do
      if not to.dead then
        local other_players = table.filter(room:getOtherPlayers(to, false), function(p)
          return not p:isRemoved()
        end)
        local luanwu_targets = table.filter(other_players, function(p2)
          return table.every(other_players, function(p1)
            return to:distanceTo(p1) >= to:distanceTo(p2)
          end)
        end)
        local use = room:askToUseCard(to, {
          pattern = "slash",
          prompt = "#lingling__luanwu-use",
          cancelable = true,
          extra_data = {
            exclusive_targets = table.map(luanwu_targets, Util.IdMapper),
            bypass_times = true,
          },
          skill_name = "lingling__luanwu",
        })
        if use then
          use.extraUse = true
          room:useCard(use)
        else
          room:loseHp(to, 1, "lingling__luanwu")
        end
      end
    end
  end,
})

return guni
